package presentation.optionForm_UI;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

@SuppressWarnings("serial")
public class ChangeKeyWindow extends JDialog
{
	private JPanel panelMain = new JPanel();
	private JTextField tfKey = new JTextField();
	private JLabel lbIntro = new JLabel("Enter your key here");
	private JButton buttonOk = new JButton("OK");
	private JButton button;
	
//	private JButton[] listButton;
	private boolean changed = false;
	
	public ChangeKeyWindow(JDialog dlg, JButton button, JButton[] listButton)
	{
		super(dlg, true);
		this.button = button;
//		this.listButton = listButton;
		initComponent();
		this.setTitle("Change Key");
		this.setSize(200,112);
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		this.setLocationRelativeTo(button);
		this.setResizable(false);
	}
	
	private void initComponent()
	{
		setLayoutMain();
		setLayoutPanelMain();
		textFieldEvent();
		buttonEvent();
		windowEvent();
	}
	
	private void setLayoutPanelMain()
	{
		this.setLayout(null);
		this.add(panelMain);
		panelMain.setBounds(1,1,198,110);
	}
	
	private void setLayoutMain()
	{
		panelMain.setLayout(null);
		panelMain.add(lbIntro);
		panelMain.add(tfKey);
		panelMain.add(buttonOk);
		
		lbIntro.setBounds(10,10,100,30);
		tfKey.setBounds(10,40,100,30);
		buttonOk.setBounds(tfKey.getX() + tfKey.getWidth()+10, tfKey.getY(), 60, 30);
	}
	String temp = "";
	
	private void textFieldEvent()
	{
		tfKey.addKeyListener(new KeyAdapter() 
		{
			@Override
			public void keyPressed(KeyEvent evt)
			{
				if(tfKey.getText().length() >= 1)
				{
					tfKey.setText(tfKey.getText().substring(0,tfKey.getText().length() - 1));
				}
			}
			@Override
			public void keyReleased(KeyEvent evt)
			{
				changed = true;
				if(tfKey.getText().length() == 0)
				{
					changed = false;
				}
			}
		});
	}
	
	private void buttonEvent()
	{
		buttonOk.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
//				if(!isSameButtonCharacter(button))
					dispose();
//				else
//				{
//					JOptionPane.showMessageDialog(null, "Your chosen key is Exist, please try another key", "Message", JOptionPane.ERROR_MESSAGE);
//				}
			}
		});
	}
	
	private void windowEvent()
	{
		this.addWindowListener(new WindowAdapter() 
		{
			@Override
			public void windowClosed(WindowEvent evt )
			{
				if(changed)
					button.setText(tfKey.getText());
			}
		});
	}
	
//	public boolean isSameButtonCharacter(JButton button)
//    {
//    	for(int i = 0; i<listButton.length; i++)
//    	{
//    		if(listButton[i].getText() != button.getText())
//    		{
//    			if(listButton[i].getText().toLowerCase().equals(button.getText().toLowerCase()));
//    				return true;
//    		}
//    	}
//    	return false;
//    }
}
